package com.ruoyi.web.controller.shop.cms;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shop.cms.service.IShopCmsCategoryService;
import com.ruoyi.shop.common.core.domain.ShopCategory;

/**
 * 商城栏目Controller
 * 
 * |----------------------------------------------------------------------|
 * |favorshop [简单好用，让您喜爱的小商城]
 * |----------------------------------------------------------------------|
 * |copyright www.favorshop.cn All rights reserved.
 * |----------------------------------------------------------------------|
 * | @author xuxiang
 * |----------------------------------------------------------------------|
 * | @date 2023-05-29
 * |----------------------------------------------------------------------|
 */
@Controller
@RequestMapping("/shop/cms/category")

public class ShopCmsCategoryController extends BaseController {

	private String prefix = "shop/cms/category";

	@Autowired
	private IShopCmsCategoryService shopCmsCategoryService;

	@GetMapping()
	public String category() {
		return prefix + "/category";
	}

	@PostMapping("/list")
	@ResponseBody
	public List<ShopCategory> list(ShopCategory category) {
		List<ShopCategory> list = shopCmsCategoryService.selectCategoryList(category);
		return list;
	}

	/**
	 * 新增分类
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存栏目
	 */
	@RequiresPermissions("shop:category:add")
	@Log(title = "栏目", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ShopCategory category) {
		category.setCreateBy(getLoginName());
		return toAjax(shopCmsCategoryService.insertCategory(category));
	}

	/**
	 * 修改栏目
	 */
	@GetMapping("/edit/{categoryId}")
	public String edit(@PathVariable("categoryId") Long categoryId, ModelMap mmap) {
		ShopCategory category = shopCmsCategoryService.selectCategoryById(categoryId);
		mmap.put("category", category);
		return prefix + "/edit";
	}

	/**
	 * 修改保存栏目
	 */
	@RequiresPermissions("shop:category:edit")
	@Log(title = "栏目", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ShopCategory category) {
		category.setUpdateBy(getLoginName());
		return toAjax(shopCmsCategoryService.updateCategory(category));
	}

	/**
	 * 删除栏目
	 */
	@RequiresPermissions("shop:category:remove")
	@Log(title = "栏目", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{categoryId}")
	@ResponseBody
	public AjaxResult remove(@PathVariable("categoryId") Long categoryId) {
		if (shopCmsCategoryService.selectCategoryCount(categoryId) > 0) {
			return AjaxResult.warn("存在下级栏目,不允许删除");
		}
		return toAjax(shopCmsCategoryService.deleteCategoryById(categoryId));
	}

	/**
	 * 选择分类树
	 * 
	 * @param categoryId 分类ID
	 * @param excludeId  排除ID
	 */
	@GetMapping(value = { "/selectCategoryTree/{categoryId}", "/selectCategoryTree/{categoryId}/{excludeId}" })
	public String selectCategoryTree(@PathVariable("categoryId") Long categoryId,
			@PathVariable(value = "excludeId", required = false) String excludeId, ModelMap mmap) {
		ShopCategory category = new ShopCategory();
		if (categoryId == 0) {
			category.setCategoryId(categoryId);
			category.setCategoryName("无");
		} else {
			category = shopCmsCategoryService.selectCategoryById(categoryId);
		}
		mmap.put("category", category);
		mmap.put("excludeId", excludeId);
		return prefix + "/tree";
	}

	/**
	 * 加载分类列表树（排除下级）
	 */
	@GetMapping("/treeData/{excludeId}")
	@ResponseBody
	public List<Ztree> treeDataExcludeChild(@PathVariable(value = "excludeId", required = false) Long excludeId) {
		ShopCategory category = new ShopCategory();
		category.setExcludeId(excludeId);
		List<Ztree> ztrees = shopCmsCategoryService.selectCategoryTreeExcludeChild(category);
		return ztrees;
	}

}
